/************************************
 * Package: RGcpp					*
 * Authors: Rui Eduardo Gouveia Gil	*
 * Date: 2010						*
 ************************************/
#ifndef __ZIPFILE_H__
#define __ZIPFILE_H__

#include <zip.h>
#include <vector>
#include <string>
#include <fstream>
#include <iostream>
#include <boost/filesystem/operations.hpp>

#include "rgcpp/exceptions/RuntimeException.h"

enum SearchType
{
	ZIP_NONE=0,
	ZIP_NOCASE,
	ZIP_NODIR,
};

namespace rgcpp
{

class ZipFile
{
	private:
		struct zip* archive;
		struct zip_file* fileStream;
		std::string archivePath;

	public:
		ZipFile();
		ZipFile(std::string archivePath);
		~ZipFile();

		void create(std::string archivePath);
		void open(std::string archivePath);
		bool isValid();
		void sync();
		void close();

		void addFile(std::string fileName, std::string filePath);
		bool isFile(std::string fileName);
		void replaceFile(std::string fileName, std::string filePath);
		void replaceFile(unsigned int index, std::string filePath);
		void renameFile(std::string fileName, std::string newFileName);
		void renameFile(unsigned int index, std::string newFileName);
		void deleteFile(std::string fileName);
		void deleteFile(unsigned int index);

		unsigned int getFileCount();
		std::vector<std::string> listArchive();

		std::string getFileName(unsigned int index);
		unsigned int getFileIndex(std::string fileName, char flag=ZIP_NONE);
		std::string getCompleteFileName(std::string fileName);
		int getFileSize(std::string fileName);
		int getFileSize(unsigned int index);
		int getFileDiskSize(std::string fileName);
		int getFileDiskSize(unsigned int index);

		void openFileStream(std::string fileName);
		void openFileStream(unsigned int index);
		int readFromFile(char* buffer, unsigned int size);
		void closeFileStream();

		void extractFile(std::string fileName, std::string dirPath);
		void extractFileToFile(std::string fileName, std::string filePath);
		void extractAllFile(std::string dirPath);

		void print();
};

}

#endif
